/**
 * Created by LinJe on 2015/10/22.
 */
;(function( $, window ) {

    var olistBox = $('.js-list-box'),
        oLoadMore = $('.js-loadmore'),
        pageStep = 1, totalSize = -1;

    //默认加载
    initLoad();

    //开启页面滚动到底部自动加载更多商品
    $.addScrollToBottom();
    $(window).on('scrollToBottom', function() {
        pageStep ++;
        var opts = {'insertFunction':'append', 'pageStep':pageStep, 'count':20};
        inserProductContent(opts, function( res ) {
            if ( res.scrollEnd ) {
                oLoadMore.removeClass('mtb300').addClass('mtb50').html('<span class="loading"><a>已经是最后一条内容</a></span>');
            } else {
                $.canloadInfo = true;
            }
        });
    });


    //点击开启提示或关闭提示
    olistBox.on('click', '.button .js-button', function(){

        if ( !userServers.getUserInfo().userName ) {
            window.globalLayer.openLogin(); //打开登陆提示弹窗
            return;
        }

        var thisBtn = $(this), needOpen = false;
        needOpen = !thisBtn.hasClass('open');
        partinprodServer.changeRemindData({
            'partPid': thisBtn.parents('li').eq(0).attr('_partpid'),
            'isOpen': needOpen
        }, function( msg ) {
            if ( msg.error || msg.code != 200 ) {
                window.globalLayer.openWarning({'autoClose':true, time: 2000, text:msg.description});
                return;
            }
            if ( needOpen ) {
                thisBtn.addClass('open').text('已开启提示').parents('.con').eq(0).find('.num').show();
            } else {
                thisBtn.removeClass('open').text('提示已关闭').parents('.con').eq(0).find('.num').hide();
            }
        });
    });


    //***********公用方法***************
    //初始化加载
    function initLoad() {
        pageStep = 1;
        totalSize = -1;
        oLoadMore.show().removeClass('mtb50').addClass('mtb300').html('<span class="loading"><a><i class="i-05"></i>正在加载更多</a></span>');
        var opts = {
            'insertFunction':'html',
            'pageStep': pageStep,
            'count': 20
        };
        inserProductContent(opts, function( res ) {

            if ( res.scrollEnd ) {
                //oLoadMore.hide();
                oLoadMore.removeClass('mtb300').addClass('mtb50').html('<span class="loading"><a>已经是最后一条内容</a></span>');
            }
        });
    }

    //插入商品内容
    function inserProductContent( opts, callBack ) {
        if (totalSize != -1 && totalSize < pageStep ) {
            callBack && callBack({'scrollEnd':true});
            return;
        }
        partinprodServer.getProductData(opts, function( msg ) {
            if ( msg.error ) {
                window.globalLayer.openWarning({'autoClose':true, time: 2000, text:msg.description});
                return;
            }
            if ( msg.code == 200 ) {
                //获取数据成功
                olistBox[opts.insertFunction]( getMoreHtml(msg.detail.participations) );
                totalSize = msg.detail.totalSize;
                callBack && callBack({'scrollEnd': totalSize <= 1});
            } else {
                window.globalLayer.openWarning({'autoClose':true, time: 2000, text:msg.description});
                callBack && callBack();
            }
        });
    }

    //获取多条商品的html
    function getMoreHtml( data ) {
        var totalHtml = '';

        for ( var i = 0, iLen = data.length; i < iLen; i ++ ) {
            totalHtml += getSingelHtml(data[i]);
        }
        return totalHtml;
    }

    //获取单条商品的html
    function getSingelHtml( data ) {
        return '<li _partpid="'+ data.partPid +'">\
                    <div class="item-box">\
                        <div class="con">\
                            <span class="num"'+(function(){
                return data.isOpen == 0 ? ' style="display:none"' : '';
            })()+'>'+ data.promptNumber +'</span>\
                            <div class="img"><div class="center"><img src="'+ data.prodPic +'?imageMogr2/thumbnail/160x160"></div></div>\
                            <div class="text">\
                                <h2>'+ data.prodTitle +'</h2>\
                                <div class="text-right"><span><a href="'+ data.prodUrl +'">进入商品讨论页 ></a></span></div>\
                            </div>\
                            <div class="button">'+(function(){
                var str = '';
                if ( data.isOpen == 0 ) {
                    str = '<button type="button" class="js-button button-default">提示已关闭</button>';
                } else {
                    str = '<button type="button" class="js-button button-default open">已开启提示</button>';
                }
                return str;
            })()+'</div>\
                        </div>\
                    </div>\
                    <div class="bottom"></div>\
                 </li>';
    }

})( jQuery, window );